package org.zfes.snowier.zbss.xzy.configer;


import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.zfes.snowier.core.exceptions.AppRuntimeException;
import org.zfes.snowier.xyz.analysis.queue.AnalyQueueBucket;
import org.zfes.snowier.xyz.analysis.queue.MemoryAnalyQueueBucket;
import org.zfes.snowier.zbss.xzy.cache.DefaultSingleDataCache;
import org.zfes.snowier.zbss.xzy.cache.ISingleDataCache;
import org.zfes.snowier.zbss.xzy.cache.devinfo.DevDataMemCache;
import org.zfes.snowier.zbss.xzy.cache.devinfo.DevDataRedisCache;
import org.zfes.snowier.zbss.xzy.cache.devinfo.IDevInfoCache;
import org.zfes.snowier.zbss.xzy.cache.mo.dev.IMoDevDataCache;
import org.zfes.snowier.zbss.xzy.cache.mo.dev.MoDevDataMemCache;
import org.zfes.snowier.zbss.xzy.cache.mo.dev.MoDevDataRedisCache;
import org.zfes.snowier.zbss.xzy.cache.mo.topo.IMoTopoDataCache;
import org.zfes.snowier.zbss.xzy.cache.mo.topo.MoTopoDataMemCache;
import org.zfes.snowier.zbss.xzy.cache.mo.topo.MoTopoDataRedisCache;
import org.zfes.snowier.zbss.xzy.store.meta.MemoryMetaCache;
import org.zfes.snowier.zbss.xzy.store.meta.RedisMetaCache;
import org.zfes.snowier.zbss.xzy.store.meta.TableMetaCache;
import org.zfes.snowier.zbss.xzy.store.queue.PersistBuketManager;
import org.zfes.snowier.zbss.xzy.store.repository.api.DataRepository;
import org.zfes.snowier.zbss.xzy.store.repository.api.IDDLService;
import org.zfes.snowier.zbss.xzy.store.repository.db.mysql.MySqlRepository;
import org.zfes.snowier.zbss.xzy.store.repository.db.mysql.MysqlDDLService;

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@EnableConfigurationProperties(value={MoProperties.class})
public class MoAutoConfiguration {
 
    
	private final MoProperties properties;
	
	  public MoAutoConfiguration(MoProperties properties) {
			this.properties = properties;
	  }
	  
	   @Bean("persistBuketManager")
	   @DependsOn({"dataRepository"})
	    protected PersistBuketManager queueManager() {
		  return new PersistBuketManager();
	    }
//	    @Bean
//	    protected IDataCache dataCache() {
//	    	String str=properties.getAnalysisDataCache();
//	    	switch (str) {
//			case "memory":
//				 return new MemoryDataCache();
//			case "redis":
//				return new RedisDataCache();
//			default:
//				throw new AppRuntimeException("不支持的AnalysisDataCache");
//			}
//	    }
	   @Bean("moTopoDataCache")
	    protected IMoTopoDataCache moTopoDataCache() {
	    	String str=properties.getDataCache();
	    	switch (str) {
			case "memory":
				 return new MoTopoDataMemCache();
			case "redis":
				return new MoTopoDataRedisCache();
			default:
				throw new AppRuntimeException("不支持的MoTopoDataCache");
			}
	    }
	    @Bean("devInfoCache")
	    protected IDevInfoCache devInfoCache() {
	    	String str=properties.getDataCache();
	    	switch (str) {
			case "memory":
				 return new DevDataMemCache();
			case "redis":
				return new DevDataRedisCache();
			default:
				throw new AppRuntimeException("不支持的AnalysisDataCache");
			}
	    }
	    
	    @Bean("modataCache")
	    protected IMoDevDataCache moDataCache() {
	    	String str=properties.getDataCache();
	    	switch (str) {
			case "memory":
				 return new MoDevDataMemCache();
			case "redis":
				return new MoDevDataRedisCache();
			default:
				throw new AppRuntimeException("不支持的AnalysisDataCache");
			}
	    }
	    
	    
	    @Bean("singleDataCache")
	    @DependsOn({"modataCache","devInfoCache"})
	    protected ISingleDataCache singleDataCache() {
	    	 return new DefaultSingleDataCache();
			
	    }
	    
	    
	    
	    @Bean
	    protected AnalyQueueBucket analysisQueue() {
	    	String str=properties.getAnalysisQueue();
	    	switch (str) {
			case "memory":
				 return new MemoryAnalyQueueBucket();
			case "redis":
			//	return new RedisAnalyQueueBucket();
			default:
				throw new AppRuntimeException("不支持的AnalysisQueue");
			}
	    }
	    @Bean("tableMetaCache")
	    protected TableMetaCache metaCache() {
	    	String str=properties.getAnalysisQueue();
	    	switch (str) {
			case "memory":
				 return new MemoryMetaCache();
			case "redis":
				return new RedisMetaCache();
			default:
				throw new AppRuntimeException("不支持的MetaCache");
			}
	    }
	    @Bean("ddlService")
	    protected IDDLService storeWhere() {
	    	String storeWhere=properties.getStoreWhere();
	    	switch (storeWhere) {
			case "mysql":
				 return new MysqlDDLService(properties.getDbName());
			case "es":
				throw new AppRuntimeException("不支持的MetaCache");
			default:
				throw new AppRuntimeException("不支持的MetaCache");
			}
	    }
	   
	    @Bean("dataRepository")
	    @DependsOn({"ddlService","tableMetaCache"})
	    protected DataRepository dataRepository() {
	    	String storeWhere=properties.getStoreWhere();
	    	switch (storeWhere) {
			case "mysql":
				 return new MySqlRepository();
			case "es":
				throw new AppRuntimeException("不支持的storeWhere");
			default:
				throw new AppRuntimeException("不支持的storeWhere");
			}
	    }
	   
	    
}
